package com.feicheng.iou.controller;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.PaymentOptions;
import com.feicheng.iou.entity.ProjectService;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.PaymentOptionsService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/paymentOptions")
@Slf4j
@Setter
@CrossOrigin
public class PaymentOptionsController {

    @Autowired
    PaymentOptionsService paymentOptionsService;

    /**
     * @Author luoxin
     * @Description 查询分期付款服务信息列表
     * @Date 2:13 下午 2021/9/1
     * @param
     * @return
     */
    @GetMapping("/list")
    public ResultInfoV110 selectPaymentOptionList() {  //查询线索列表
        log.info("selectPaymentOptionList方法进入");

        //调用ResourceService实现线索列表查询业务逻辑
        return paymentOptionsService.selectPaymentOptionsServiceList();
    }

    /**
     * @Author luoxin
     * @Description TODO
     * @Date 9:29 上午 2021/9/2
     * @param null
     * @return
     */
    @GetMapping("/listPage")
    public ResultInfoV110 selectPaymentOptionListPage(Integer page,Integer size,String paymentOptionsStr) {  //查询线索列表
        log.info("selectPaymentOptionListPage方法进入");

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(paymentOptionsStr);
        PaymentOptions paymentOptions = jsonObject.toJavaObject(PaymentOptions.class);

        //调用ResourceService实现线索列表查询业务逻辑
        return paymentOptionsService.selectPaymentOptionsListPage(page,size,paymentOptions);
    }
    /**
     * @Author luoxin
     * @Description 根据Id查询分期付款服务信息
     * @Date 2:13 下午 2021/9/1
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public ResultInfoV110 selectPaymentOptioninfo(@PathVariable("id") Integer id) {  //查询线索列表
        log.info("selectPaymentOptioninfo方法进入，参数为"+id);

        //调用ResourceService实现线索列表查询业务逻辑
        return ResultInfoV110.success(paymentOptionsService.selectPaymentOptionsByid(id));
    }

    /**
     * @Author luoxin
     * @Description 添加分期付款服务信息
     * @Date 2:13 下午 2021/9/1
     * @param paymentOptionStr
     * @return
     */
    @PostMapping
    public ResultInfoV110 addPaymentOption(String paymentOptionStr){
        log.info("addPaymentOption方法进入,参数为"+paymentOptionStr);
        //判断请求是否重复
        if (!IdempotentUtils.judge(paymentOptionStr, this.getClass())) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004, "分期付款服务添加"));
        }

        // 参数校验是否为空
        if (paymentOptionStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "分期付款服务"));
        }

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(paymentOptionStr);
        PaymentOptions paymentOption = jsonObject.toJavaObject(PaymentOptions.class);

        return  paymentOptionsService.insert(paymentOption);
    }

    /**
     * @Author luoxin
     * @Description 根据Id修改分期付款服务信息
     * @Date 2:13 下午 2021/9/1
     * @param paymentOptionStr
     * @return
     */
    @PutMapping
    public ResultInfoV110 updatePaymentOption(String paymentOptionStr){
        log.info("updatePaymentOption方法进入,参数为"+paymentOptionStr);

        // 参数校验是否为空
        if (paymentOptionStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "分期付款服务"));
        }

        //字符串转行成对象
        JSONObject jsonObject = JSONObject.parseObject(paymentOptionStr);
        PaymentOptions paymentOption = jsonObject.toJavaObject(PaymentOptions.class);

        return  paymentOptionsService.update(paymentOption);
    }

    /**
     * @Author luoxin
     * @Description 根据ID删除分期付款服务
     * @Date 2:15 下午 2021/9/1
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResultInfoV110 deletePaymentOption(@PathVariable("id") Integer id){
        log.info("deletePaymentOption方法进入,参数为"+id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "分期付款服务Id"));
        }

        return paymentOptionsService.delete(id);
    }

    @PutMapping("/status")
    public ResultInfoV110 updatePaymentOptionStatus(Integer id ,boolean status){
        log.info("updatePaymentOptionStatus方法进入,参数为" + id+"状态为"+status);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "分期服务ID"));
        }

        return  paymentOptionsService.updateStatus(id,status);
    }

}

